﻿@model List<BusinessLayer.RegInfo.Entities.Subscription>
@{
    ViewBag.Title = "List Subscription";
}

<h2>
        List Subscription</h2>
    <p style="width: 95%;">
        <a class="bt_green" href="/Subscription/Create"><span class="bt_green_lft"></span><strong>Create</strong><span class="bt_green_r"></span></a>
        <a class="bt_red" href="#" onclick="DeleteSelectedItems(this);"><span class="bt_red_lft"></span><strong>Delete items</strong><span class="bt_red_r"></span></a>
    </p>
    <p>
        @if (ViewData["ErrorMessage"] != null)
        {
            @Html.Label(ViewData["ErrorMessage"].ToString())
        }
    </p>
    <table id="rounded-corner"><thead>
        <tr>
            <th class="rounded-company" scope="col">
                <input id="chkAll" onclick="javascript:SelectAll(this);" type="checkbox" />
            </th>
            <th class="rounded" scope="col">
                Code
            </th>
            <th class="rounded" scope="col">
                Title
            </th>
            <th class="rounded" scope="col">
                Number of days
            </th>
            <th class="rounded" scope="col">
                Price
            </th>
            <th class="rounded" scope="col">
                Status
            </th>
            <th class="rounded-q4" scope="col">
            </th>
        </tr>
        @foreach (var item in Model)
           { 
        <tr>
            <td>
                @Html.CheckBox("check", new { itemid = item.SubscriptionID, refer = "chk", onclick = "javascript:SelectOne(this)" })
            </td>
            <td>
                @item.Code
            </td>
            <td>
                @item.Title 
            </td>
            <td>
                @item.NumberOfDays 
            </td>
            <td>
                @String.Format("{0:F}", item.Price) 
            </td>
            <td>
                @BackEnd.Common.Constants.Status.GetStringStatus(item.Status) 
            </td>
            <td>                
                <a href="/Subscription/Edit?id=@item.SubscriptionID"><img src="/Content/Images/user_edit.png" /></a>
            </td>
        </tr>
       }</thead> 
    </table>
    <input id="hidSubscriptionIDs" type="hidden" />
    <script type="text/javascript">
        /*Check, Check All*/
        function SelectAll(spanChk) {

            var hidSelected = $("input[id='hidSubscriptionIDs']");
            var chkList = $("input[refer='chk']");
            var str = "";

            for (var i = 0; i < chkList.length; i++) {
                var chk = chkList[i];
                chk.checked = spanChk.checked;
                if (chk.checked) {
                    str = str + $(chk).attr("itemid") + "_";
                }
            }
            str = str == "" ? "" : str.substring(0, str.length - 1);
            hidSelected.attr("value", str);
            //alert(hidSelected.val());
        }
        function SelectOne() {
            var hidSelected = $("input[id='hidSubscriptionIDs']");
            var chkList = $("input[refer='chk']");
            var str = "";

            for (var i = 0; i < chkList.length; i++) {
                var chk = chkList[i];
                if (chk.checked) {
                    str = str + $(chk).attr("itemid") + "_";
                }
            }
            str = str == "" ? "" : str.substring(0, str.length - 1);
            hidSelected.attr("value", str);
        }
        function DeleteSelectedItems() {
            
            if (confirm('Are you sure that want delete these items?')) {
                var hidSelected = $("input[id='hidSubscriptionIDs']");
                if (hidSelected.val() != "") {

                    var rows = $("input:checked");
                    rows.each(function () {
                        $(this).parent().parent().remove();
                    });
                    
                    var URL = '/Subscription/Delete/' + hidSelected.val();
                    $.post(URL, function (data) {
                        //                        alert(data);
                        //                        if (data == "1") {
                        //                            alert("Delete successful");
                        //                            
                        //                        }
                        //                        else {
                        //                            alert("Cannot delete selected items!");
                        //                        }
                    });

                }
            }
        }
    </script>
